package com.yanqu.road.dao.impl.recharge;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;

import java.sql.*;

public class PlatformRewardDaoImpl extends TempDao {

	public boolean addPlatformReward(String orderNo, long userId, int goodsId, int count, int type, String platformPrice) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_platform_reward(orderNo,userId,goodsId,count,createTime,type,platformPrice) VALUES (?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, orderNo);
			call.setLong(i++, userId);
			call.setInt(i++, goodsId);
			call.setInt(i++, count);
			call.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
			call.setInt(i++, type);
			call.setString(i++, platformPrice);
            return call.executeUpdate() > 0;
		} catch (SQLException e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	public boolean checkPlatformRewardOrder(String orderNo) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_platform_reward where orderNo=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, orderNo);
			rs = call.executeQuery();
			while (rs.next()) {
				return true;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return false;
	}
}
